package cn.itcast.web.config;


import cn.itcast.web.task.DeliveryNoticeTask;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

//方案一  以停用
//@Configuration
public class TaskConfig {

    @Autowired
    private DeliveryNoticeTask deliveryNoticeTask;

    @Bean
    public MethodInvokingJobDetailFactoryBean jobDetail(){
        MethodInvokingJobDetailFactoryBean jobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
        jobDetailFactoryBean.setTargetObject(deliveryNoticeTask);
        jobDetailFactoryBean.setTargetMethod("emailNotice");
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean triggerFactory(MethodInvokingJobDetailFactoryBean jobDetail){
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setCronExpression("0 6 * * * ?");
        //测试
//        cronTriggerFactoryBean.setCronExpression("0/10 * * * * ?");
        cronTriggerFactoryBean.setJobDetail(jobDetail.getObject());
        return cronTriggerFactoryBean;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean triggerFactory){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(triggerFactory.getObject());
        return schedulerFactoryBean;
    }
}
